﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:FlashPlayer runat=server></{0}:FlashPlayer>")]
    public class FlashPlayer : WebControl
    {

        [Category("Appearance")]
        public FlashMovieQuality FlashMovieQuality
        {
            get
            {
                object obj = ViewState["FlashMovieQuality"];
                if (obj == null) { return FlashMovieQuality.High; }
                return (FlashMovieQuality)obj;
            }
            set { ViewState["FlashMovieQuality"] = value; }
        }

        [Category("Appearance")]
        public FlashScaleMode FlashScaleMode
        {
            get
            {
                object obj = ViewState["FlashScaleMode"];
                if (obj == null) { return FlashScaleMode.Normal; }
                return (FlashScaleMode)obj;
            }
            set { ViewState["FlashScaleMode"] = value; }
        }

        [Category("Appearance")]
        public bool ShowMenu
        {
            get
            {
                object obj = ViewState["ShowMenu"];
                if (obj == null) { return true; }
                return (bool)obj;
            }
            set { ViewState["ShowMenu"] = value; }
        }

        [Category("Appearance")]
        public bool Loop
        {
            get
            {
                object obj = ViewState["Loop"];
                if (obj == null) { return true; }
                return (bool)obj;
            }
            set { ViewState["Loop"] = value; }
        }

        [Category("Data")]
        public string FlashVariables
        {
            get
            {
                object obj = ViewState["FlashVariables"];
                if (obj == null) { return string.Empty; }
                return obj.ToString();
            }
            set { ViewState["FlashVariables"] = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        public string FlashUrl
        {
            get
            {
                object s = ViewState["FlashUrl"];
                if (s == null) { return string.Empty; }
                return s.ToString();
            }

            set
            {
                ViewState["FlashUrl"] = value;
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.AddAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
            output.AddAttribute("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0");
            //output.AddAttribute("codebase","http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0");

            output.AddAttribute("width", this.Width.Value.ToString());
            output.AddAttribute("height", this.Height.Value.ToString());
            output.AddAttribute("id", this.ClientID);
            output.RenderBeginTag(HtmlTextWriterTag.Object);

            AddParameter(output, "movie", this.FlashUrl);
            AddParameter(output, "quality", this.InternalFlashMovieQuality);
            AddParameter(output, "scale", this.InternalFlashScaleMode);

            if (this.ShowMenu) { AddParameter(output, "menu", "true"); } else { AddParameter(output, "menu", "false"); }
            if (this.Loop) { AddParameter(output, "loop", "true"); } else { AddParameter(output, "loop", "false"); }
            if (!this.BackColor.IsEmpty) { AddParameter(output, "bgcolor", System.Drawing.ColorTranslator.ToHtml(this.BackColor)); }
            if (this.FlashVariables.Trim() != string.Empty) { AddParameter(output, "flashvars", this.FlashVariables.Trim()); }

            output.AddAttribute("href", this.FlashUrl);
            output.AddAttribute("quality", this.InternalFlashMovieQuality);
            output.AddAttribute("scale", this.InternalFlashScaleMode);
            if (!this.BackColor.IsEmpty) { output.AddAttribute("bgcolor", System.Drawing.ColorTranslator.ToHtml(this.BackColor)); }
            if (this.ShowMenu) { output.AddAttribute("menu", "true"); } else { output.AddAttribute("menu", "false"); }
            if (this.Loop) { output.AddAttribute("loop", "true"); } else { output.AddAttribute("loop", "false"); }

            output.AddAttribute("width", this.Width.Value.ToString());
            output.AddAttribute("height", this.Height.Value.ToString());
            output.AddAttribute("align", string.Empty);
            output.AddAttribute("name", this.ClientID);
            if (this.FlashVariables.Trim() != string.Empty) { output.AddAttribute("flashvars", this.FlashVariables.Trim()); }
            output.AddAttribute("type", "application/x-shockwave-flash");
            output.AddAttribute("pluginspace", "http://www.macromedia.com/go/getflashplayer");
            output.RenderBeginTag(HtmlTextWriterTag.Embed);
            output.RenderEndTag();
            output.RenderEndTag();
        }

        private void AddParameter(HtmlTextWriter output, string name, string value)
        {
            output.AddAttribute("name", name);
            output.AddAttribute("value", value);
            output.RenderBeginTag(HtmlTextWriterTag.Param);
            output.RenderEndTag();

        }

        private string InternalFlashMovieQuality
        {
            get
            {
                switch (this.FlashMovieQuality)
                {
                    case FlashMovieQuality.AutoHigh: return "autohigh";
                    case FlashMovieQuality.AutoLow: return "autolow";
                    case FlashMovieQuality.Best: return "best";
                    case FlashMovieQuality.High: return "high";
                    case FlashMovieQuality.Low: return "low";
                    case FlashMovieQuality.Medium: return "medium";
                }

                return "best";
            }
        }

        private string InternalFlashScaleMode
        {
            get
            {
                switch (this.FlashScaleMode)
                {
                    case FlashScaleMode.ExactFit: return "exactfit";
                    case FlashScaleMode.NoOrder: return "noroder";
                    case FlashScaleMode.Normal: return "default";
                }
                return "default";
            }
        }

    }

    public enum FlashMovieQuality
    {
        Best, Low, AutoLow, AutoHigh, Medium, High
    }

    public enum FlashScaleMode
    {
        Normal, NoOrder, ExactFit
    }

}